import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from './router'
import store from './store'
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  // 开启进度条
  NProgress.start()
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      /*  if (!store.getters.userId) {
        await store.dispatch('user/getProfile')
      } */
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      Message.error('请先登录')
      next('/login')
    }
  }
})
// 后置守卫
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
